<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="matching-searching.html" />
<link rel="prev" href="module-re.html" />
<link rel="parent" href="module-re.html" />
<link rel="next" href="matching-searching.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>4.2.1 正则表达式语法</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="4.2 re  "
  href="module-re.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="4.2 re  "
  href="module-re.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="4.2.2 matching vs Searching"
  href="matching-searching.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python库参考</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">前一节:</b>
<a class="sectref" rel="prev" href="module-re.html">4.2 re  </a>
<b class="navlabel">上一级:</b>
<a class="sectref" rel="parent" href="module-re.html">4.2 re  </a>
<b class="navlabel">下一节:</b>
<a class="sectref" rel="next" href="matching-searching.html">4.2.2 匹配 vs 查找</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION006210000000000000000"></a><a name="re-syntax"></a>
<br>
4.2.1 正则表达式语法</h2>

<p>
正则表达式（或RE）指定了一套匹配的字符串；这个模块中的函数让你检查一个特别的字符串是否匹配一个给定的正则表达式（或一个给定的正则表达式是否匹配一个特别的字符串，归结为同样的事情）。
<p>
正则表达式可以被连接形成新的正则表达式；如果<em> A </em>和<em> B</em> 都是正则表达式，那么 <em>AB</em> 也是一个正则表达式。一般而言，如果一个字符串
<em>p</em> 匹配<em> A</em>，另一个字符串 <em>q</em> 匹配 <em>B</em>，字符串 <em>pq</em> 将匹配 AB。这除非 <em>A</em> 或<em> B</em> 包含低优先级的操作；
<em>A </em>和 <em>B</em> 之间的边界条件；或拥有编号组参考。因此，像这儿描述的一个，从简单的原始的表达式中，复杂的表达式可以被很容易的构造。正则表达式理论和实现的详情，查阅前面引用的Friedl的书籍，或几乎所有的关于编译构造的教科书。.

<p>
一个正则表达式遵循的格式的简短说明。更多信息和一个优雅的说明，查阅如何正则表达式，访问<a class="url" href="http://www.python.org/doc/howto/">http://www.python.org/doc/howto/</a>.

<p>
正则表达式可以包含特殊的和普通的字符。多数普通字符，像 "<tt class="character">A</tt>"，"<tt class="character">a</tt>"，或
"<tt class="character">0</tt>"，是最简单的正则表达式； 它们简单地匹配自身。你可以连接原始的字符，因此 <tt class="regexp">last </tt>匹配字符串<code>'last'</code>。(这节剩余的部分中，我们将以特殊的格式编写RE，通常不用引号，并且被匹配的字符串 <code>'用单引号'</code>。)
<p>一些字符，像 "<tt class="character">|</tt>" 或 "<tt class="character">(</tt>"，是特殊的。特殊的字符不是代表原始字符的类，就是影响它们周围的正则表达式如何被解释。
<p>
特殊的字符是：
<dl>
<dt><strong>"<tt class="character">.</tt>"</strong></dt>
<dd>（点。）缺省的模式中，匹配除换行以外的所有字符。如果<tt class="constant">DOTALL</tt> 标志已经被指定，匹配包括换行在内的所有字符。

<p>
</dd>
<dt><strong>"<tt class="character">^</tt>"</strong></dt>
<dd>（脱字符，或向上的箭头）匹配字符串的起始，在 <tt class="constant">MULTILINE</tt>模式中，在每个换行后也立即匹配。
  <p>
</dd>
<dt><strong>"<tt class="character">$</tt>"</strong></dt>
<dd>匹配字符串的结尾或换行之前字符串的结尾，<tt class="constant">MULTILINE</tt>模式中换行之前也匹配。当正则表达式 <tt class="regexp">foo$</tt> 仅匹配
'foo' 时，<tt class="regexp">foo</tt> 既匹配 'foo' 又匹配
'foobar'。更有意思，在
'foo1\nfoo2\n' 中查找 <tt class="regexp">foo.$</tt> 通常匹配 'foo2' ，但是在 <tt class="constant">MULTILINE </tt>模式中匹配 'foo1'。

<p>
</dd>
<dt><strong>"<tt class="character">*</tt>"</strong></dt>
<dd>使由此产生的 RE 匹配 0 或多个处于 RE 之前的重复， 尽可能多的重复。  <tt class="regexp">ab*</tt> 匹配 'a'，'ab'，或 'a' 后跟随任意数目的 'b'。

  <p>
</dd>
<dt><strong>"<tt class="character">+</tt>"</strong></dt>
<dd>使由此产生的 RE 匹配 1 或多个处于 RE 之前的重复。<tt class="regexp">ab+</tt> 匹配 'a' 后跟随任意非零数目的 'b'； 它仅不匹配 'a'。

  <p>
</dd>
<dt><strong>"<tt class="character">?</tt>"</strong></dt>
<dd>使由此产生的 RE 匹配 0 或 1 个处于 RE 之前的重复。<tt class="regexp">ab?</tt> 匹配 'a' 或 'ab'。

  <p>
</dd>
<dt><strong><code>*?</code>, <code>+?</code>, <code>??</code></strong></dt>
<dd>"<tt class="character">*</tt>"，"<tt class="character">+</tt>"，和"<tt class="character">?</tt>" 修饰符都是 <i class="dfn">贪婪的</i>； 它们匹配尽可能多的文本。  有时这种行为不是希望的； 如果 RE <tt class="regexp">&lt;.*&gt;</tt> 匹配<code>'&lt;H1&gt;title&lt;/H1&gt;'</code>，它将匹配整个字符串， 而不仅是
<code>'&lt;H1&gt;'</code>。  在修饰符后添加 "<tt class="character">?</tt>" 使它以<i class="dfn">非贪婪的</i> 或 <i class="dfn">最小的</i> 风格来执行匹配；尽可能<em> 少的 </em>字符将被匹配。  在前面的表达式中使用 <tt class="regexp">.*?</tt> 将仅匹配<code>'&lt;H1&gt;'</code>。
<p>
</dd>
<dt><strong><code>{<var>m</var>}</code></strong></dt>
<dd>具体说明应该被正确地匹配的前面 RE 的 <var>m</var> 的副本； 较少的匹配导致整个 RE 不匹配。例如，
<tt class="regexp">a{6}</tt> 将正确地匹配六个 "<tt class="character">a</tt>" 字符， 而不是五个。

<p>
</dd>
<dt><strong><code>{<var>m</var>,<var>n</var>}</code></strong></dt>
<dd>使由此产生的 RE 匹配从
<var>m</var>到<var>n</var> 的处于RE之前的重复，试图匹配尽可能多的重复。例如， <tt class="regexp">a{3,5}</tt>
将匹配从 3 到 5 的 "<tt class="character">a</tt>" 字符。省略 <var>m</var>
指定下限为零，省略 <var>n</var> 指定无限上限。举例来说，<tt class="regexp">a{4,}b</tt> 将匹配 <code>aaaab</code> 或 a <code>b </code>后紧跟 一千个 "<tt class="character">a</tt>" 字符，但不是<code>aaab</code>。逗号不能被省略，否者修改者将由于前面的描述格式而搞乱。

<p>
</dd>
<dt><strong><code>{<var>m</var>,<var>n</var>}?</code></strong></dt>
<dd>使由此产生的 RE 匹配从 <var>m</var>到<var>n</var> 的处于RE之前的重复，试图匹配尽可能少的重复。这是前面修饰符的非贪婪版本。例如，6个字符的字符串 <code>'aaaaaa'</code>，<tt class="regexp">a{3,5}</tt> 将匹配 5个 "<tt class="character">a</tt>" 字符，当 <tt class="regexp">a{3,5}?</tt> 将仅匹配 3 个字符。

  <p>
</dd>
<dt><strong>"<tt class="character">\</tt>"</strong></dt>
<dd>转换特殊字符(允许你匹配像 "<tt class="character">*</tt>", "<tt class="character">?</tt>" 一样的字符，等等)，或单独一个特殊序列；特殊序列被在下面讨论。

  <p>
  如果你不使用原始的字符串表示模式，记住Python在字符串原义上也使用反斜线作为一个转换序列；if the escape
sequence isn't recognized by Python's parser, the backslash and
subsequent character are included in the resulting string.  However,
if Python would recognize the resulting sequence, the backslash should
be repeated twice.  This is complicated and hard to understand, so
it's highly recommended that you use raw strings for all but the
simplest expressions.

<p>
</dd>
<dt><strong><code>[]</code></strong></dt>
<dd>Used to indicate a set of characters.  Characters can
be listed individually, or a range of characters can be indicated by
giving two characters and separating them by a "<tt class="character">-</tt>".  Special
characters are not active inside sets.  For example, <tt class="regexp">[akm$]</tt>
will match any of the characters "<tt class="character">a</tt>", "<tt class="character">k</tt>",
"<tt class="character">m</tt>", or "<tt class="character">$</tt>"; <tt class="regexp">[a-z]</tt>
will match any lowercase letter, and <code>[a-zA-Z0-9]</code> matches any
letter or digit.  Character classes such as <code>\w</code> or <code>\S</code>
(defined below) are also acceptable inside a range.  If you want to
include a "<tt class="character">]</tt>" or a "<tt class="character">-</tt>" inside a set, precede it with a
backslash, or place it as the first character.  The
pattern <tt class="regexp">[]]</tt> will match <code>']'</code>, for example.

<p>
You can match the characters not within a range by <i class="dfn">complementing</i>
the set.  This is indicated by including a
"<tt class="character">^</tt>" as the first character of the set;
"<tt class="character">^</tt>" elsewhere will simply match the
"<tt class="character">^</tt>" character.  For example,
<tt class="regexp">[^5]</tt> will match
any character except "<tt class="character">5</tt>", and
<tt class="regexp">[^<code>^</code>]</tt> will match any character
except "<tt class="character">^</tt>".

<p>
</dd>
<dt><strong>"<tt class="character">|</tt>"</strong></dt>
<dd><code>A|B</code>, where A and B can be arbitrary REs,
creates a regular expression that will match either A or B.  An
arbitrary number of REs can be separated by the "<tt class="character">|</tt>" in this
way.  This can be used inside groups (see below) as well.  As the target
string is scanned, REs separated by "<tt class="character">|</tt>" are tried from left to
right. When one pattern completely matches, that branch is accepted.
This means that once <code>A</code> matches, <code>B</code> will not be tested further,
even if it would produce a longer overall match.  In other words, the
"<tt class="character">|</tt>" operator is never greedy.  To match a literal "<tt class="character">|</tt>",
use <tt class="regexp">\|</tt>, or enclose it inside a character class, as in <tt class="regexp">[|]</tt>.

<p>
</dd>
<dt><strong><code>(...)</code></strong></dt>
<dd>Matches whatever regular expression is inside the
parentheses, and indicates the start and end of a group; the contents
of a group can be retrieved after a match has been performed, and can
be matched later in the string with the <tt class="regexp">\<var>number</var></tt> special
sequence, described below.  To match the literals "<tt class="character">(</tt>" or
"<tt class="character">)</tt>", use <tt class="regexp">\(</tt> or <tt class="regexp">\)</tt>, or enclose them
inside a character class: <tt class="regexp">[(] [)]</tt>.

<p>
</dd>
<dt><strong><code>(?...)</code></strong></dt>
<dd>This is an extension notation (a "<tt class="character">?</tt>"
following a "<tt class="character">(</tt>" is not meaningful otherwise).  The first
character after the "<tt class="character">?</tt>"
determines what the meaning and further syntax of the construct is.
Extensions usually do not create a new group;
<tt class="regexp">(?P&lt;<var>name</var>&gt;...)</tt> is the only exception to this rule.
Following are the currently supported extensions.

<p>
</dd>
<dt><strong><code>(?iLmsux)</code></strong></dt>
<dd>(One or more letters from the set "<tt class="character">i</tt>",
"<tt class="character">L</tt>", "<tt class="character">m</tt>", "<tt class="character">s</tt>", "<tt class="character">u</tt>",
"<tt class="character">x</tt>".)  The group matches the empty string; the letters set
the corresponding flags (<tt class="constant">re.I</tt>, <tt class="constant">re.L</tt>,
<tt class="constant">re.M</tt>, <tt class="constant">re.S</tt>, <tt class="constant">re.U</tt>, <tt class="constant">re.X</tt>)
for the entire regular expression.  This is useful if you wish to
include the flags as part of the regular expression, instead of
passing a <var>flag</var> argument to the <tt class="function">compile()</tt> function.

<p>
Note that the <tt class="regexp">(?x)</tt> flag changes how the expression is parsed.
It should be used first in the expression string, or after one or more
whitespace characters.  If there are non-whitespace characters before
the flag, the results are undefined.

<p>
</dd>
<dt><strong><code>(?:...)</code></strong></dt>
<dd>A non-grouping version of regular parentheses.
Matches whatever regular expression is inside the parentheses, but the
substring matched by the
group <em>cannot</em> be retrieved after performing a match or
referenced later in the pattern.

<p>
</dd>
<dt><strong><code>(?P&lt;<var>name</var>&gt;...)</code></strong></dt>
<dd>Similar to regular parentheses, but
the substring matched by the group is accessible via the symbolic group
name <var>name</var>.  Group names must be valid Python identifiers, and
each group name must be defined only once within a regular expression.  A
symbolic group is also a numbered group, just as if the group were not
named.  So the group named 'id' in the example above can also be
referenced as the numbered group 1.

<p>
For example, if the pattern is
<tt class="regexp">(?P&lt;id&gt;[a-zA-Z_]\w*)</tt>, the group can be referenced by its
name in arguments to methods of match objects, such as
<code>m.group('id')</code> or <code>m.end('id')</code>, and also by name in
pattern text (for example, <tt class="regexp">(?P=id)</tt>) and replacement text
(such as <code>\g&lt;id&gt;</code>).

<p>
</dd>
<dt><strong><code>(?P=<var>name</var>)</code></strong></dt>
<dd>Matches whatever text was matched by the
earlier group named <var>name</var>.

<p>
</dd>
<dt><strong><code>(?#...)</code></strong></dt>
<dd>A comment; the contents of the parentheses are
simply ignored.

<p>
</dd>
<dt><strong><code>(?=...)</code></strong></dt>
<dd>Matches if <tt class="regexp">...</tt> matches next, but doesn't
consume any of the string.  This is called a lookahead assertion.  For
example, <tt class="regexp">Isaac (?=Asimov)</tt> will match <code>'Isaac&nbsp;'</code> only if it's
followed by <code>'Asimov'</code>.

<p>
</dd>
<dt><strong><code>(?!...)</code></strong></dt>
<dd>Matches if <tt class="regexp">...</tt> doesn't match next.  This
is a negative lookahead assertion.  For example,
<tt class="regexp">Isaac (?!Asimov)</tt> will match <code>'Isaac&nbsp;'</code> only if it's <em>not</em>
followed by <code>'Asimov'</code>.

<p>
</dd>
<dt><strong><code>(?&lt;=...)</code></strong></dt>
<dd>Matches if the current position in the string
is preceded by a match for <tt class="regexp">...</tt> that ends at the current
position.  This is called a <i class="dfn">positive lookbehind assertion</i>.
<tt class="regexp">(?&lt;=abc)def</tt> will find a match in "<tt class="samp">abcdef</tt>", since the
lookbehind will back up 3 characters and check if the contained
pattern matches.  The contained pattern must only match strings of
some fixed length, meaning that <tt class="regexp">abc</tt> or <tt class="regexp">a|b</tt> are
allowed, but <tt class="regexp">a*</tt> and <tt class="regexp">a{3,4}</tt> are not.  Note that
patterns which start with positive lookbehind assertions will never
match at the beginning of the string being searched; you will most
likely want to use the <tt class="function">search()</tt> function rather than the
<tt class="function">match()</tt> function:

<p>
<div class="verbatim"><pre>
&gt;&gt;&gt; import re
&gt;&gt;&gt; m = re.search('(?&lt;=abc)def', 'abcdef')
&gt;&gt;&gt; m.group(0)
'def'
</pre></div>

<p>
This example looks for a word following a hyphen:

<p>
<div class="verbatim"><pre>
&gt;&gt;&gt; m = re.search('(?&lt;=-)\w+', 'spam-egg')
&gt;&gt;&gt; m.group(0)
'egg'
</pre></div>

<p>
</dd>
<dt><strong><code>(?&lt;!...)</code></strong></dt>
<dd>Matches if the current position in the string
is not preceded by a match for <tt class="regexp">...</tt>.  This is called a
<i class="dfn">negative lookbehind assertion</i>.  Similar to positive lookbehind
assertions, the contained pattern must only match strings of some
fixed length.  Patterns which start with negative lookbehind
assertions may match at the beginning of the string being searched.

<p>
</dd>
<dt><strong><code>(?(<var>id/name</var>)yes-pattern|no-pattern)</code></strong></dt>
<dd>Will try to match
with <tt class="regexp">yes-pattern</tt> if the group with given <var>id</var> or <var>name</var>
exists, and with <tt class="regexp">no-pattern</tt> if it doesn't. <tt class="regexp">|no-pattern</tt>
is optional and can be omitted. For example, 
<tt class="regexp">(&lt;)?(\w+@\w+(?:\.\w+)+)(?(1)&gt;)</tt> is a poor email matching
pattern, which will match with <code>'&lt;user@host.com&gt;'</code> as well as
<code>'user@host.com'</code>, but not with <code>'&lt;user@host.com'</code>.

<span class="versionnote">New in version 2.4.</span>

<p>
</dd>
</dl>

<p>
The special sequences consist of "<tt class="character">\</tt>" and a character from the
list below.  If the ordinary character is not on the list, then the
resulting RE will match the second character.  For example,
<tt class="regexp">\$</tt> matches the character "<tt class="character">$</tt>".
<dl>
<dt><strong><code>\<var>number</var></code></strong></dt>
<dd>Matches the contents of the group of the
same number.  Groups are numbered starting from 1.  For example,
<tt class="regexp">(.+) \1</tt> matches <code>'the the'</code> or <code>'55 55'</code>, but not
<code>'the end'</code> (note
the space after the group).  This special sequence can only be used to
match one of the first 99 groups.  If the first digit of <var>number</var>
is 0, or <var>number</var> is 3 octal digits long, it will not be interpreted
as a group match, but as the character with octal value <var>number</var>.
Inside the "<tt class="character">[</tt>" and "<tt class="character">]</tt>" of a character class, all numeric
escapes are treated as characters.

<p>
</dd>
<dt><strong><code>\A</code></strong></dt>
<dd>Matches only at the start of the string.

<p>
</dd>
<dt><strong><code>\b</code></strong></dt>
<dd>Matches the empty string, but only at the
beginning or end of a word.  A word is defined as a sequence of
alphanumeric or underscore characters, so the end of a word is indicated by
whitespace or a non-alphanumeric, non-underscore character.  Note that 
<code>\b</code> is defined as the boundary between <code>\w</code> and <code>\
W</code>, so the precise set of characters deemed to be alphanumeric depends on the
values of the <code>UNICODE</code> and <code>LOCALE</code> flags.  Inside a character
range, <tt class="regexp">\b</tt> represents the backspace character, for compatibility
with Python's string literals.

<p>
</dd>
<dt><strong><code>\B</code></strong></dt>
<dd>Matches the empty string, but only when it is <em>not</em>
at the beginning or end of a word.  This is just the opposite of <code>\
b</code>, so is also subject to the settings of <code>LOCALE</code> and <code>UNICODE</code>.

<p>
</dd>
<dt><strong><code>\d</code></strong></dt>
<dd>When the <tt class="constant">UNICODE</tt> flag is not specified, matches
any decimal digit; this is equivalent to the set <tt class="regexp">[0-9]</tt>. 
With <tt class="constant">UNICODE</tt>, it will match whatever is classified as a digit
in the Unicode character properties database.

<p>
</dd>
<dt><strong><code>\D</code></strong></dt>
<dd>When the <tt class="constant">UNICODE</tt> flag is not specified, matches
any non-digit character; this is equivalent to the set 
<tt class="regexp">[^0-9]</tt>.  With <tt class="constant">UNICODE</tt>, it will match 
anything other than character marked as digits in the Unicode character 
properties database.

<p>
</dd>
<dt><strong><code>\s</code></strong></dt>
<dd>When the <tt class="constant">LOCALE</tt> and <tt class="constant">UNICODE</tt>
flags are not specified, matches any whitespace character; this is
equivalent to the set <tt class="regexp">[ \t\n\r\f\v]</tt>.
With <tt class="constant">LOCALE</tt>, it will match this set plus whatever characters
are defined as space for the current locale. If <tt class="constant">UNICODE</tt> is set,
this will match the characters <tt class="regexp">[ \t\n\r\f\v]</tt> plus
whatever is classified as space in the Unicode character properties
database.

<p>
</dd>
<dt><strong><code>\S</code></strong></dt>
<dd>When the <tt class="constant">LOCALE</tt> and <tt class="constant">UNICODE</tt>
flags are not specified, matches any non-whitespace character; this is
equivalent to the set <tt class="regexp">[^ \t\n\r\f\v]</tt>
With <tt class="constant">LOCALE</tt>, it will match any character not in this set,
and not defined as space in the current locale. If <tt class="constant">UNICODE</tt>
is set, this will match anything other than <tt class="regexp">[ \t\n\r\f\v]</tt>
and characters marked as space in the Unicode character properties database.

<p>
</dd>
<dt><strong><code>\w</code></strong></dt>
<dd>When the <tt class="constant">LOCALE</tt> and <tt class="constant">UNICODE</tt>
flags are not specified, matches any alphanumeric character and the
underscore; this is equivalent to the set
<tt class="regexp">[a-zA-Z0-9_]</tt>.  With <tt class="constant">LOCALE</tt>, it will match the set
<tt class="regexp">[0-9_]</tt> plus whatever characters are defined as alphanumeric for
the current locale.  If <tt class="constant">UNICODE</tt> is set, this will match the
characters <tt class="regexp">[0-9_]</tt> plus whatever is classified as alphanumeric
in the Unicode character properties database.

<p>
</dd>
<dt><strong><code>\W</code></strong></dt>
<dd>When the <tt class="constant">LOCALE</tt> and <tt class="constant">UNICODE</tt>
flags are not specified, matches any non-alphanumeric character; this
is equivalent to the set <tt class="regexp">[^a-zA-Z0-9_]</tt>.   With
<tt class="constant">LOCALE</tt>, it will match any character not in the set
<tt class="regexp">[0-9_]</tt>, and not defined as alphanumeric for the current locale.
If <tt class="constant">UNICODE</tt> is set, this will match anything other than
<tt class="regexp">[0-9_]</tt> and characters marked as alphanumeric in the Unicode
character properties database.

<p>
</dd>
<dt><strong><code>\Z</code></strong></dt>
<dd>Matches only at the end of the string.

<p>
</dd>
</dl>

<p>
Most of the standard escapes supported by Python string literals are
also accepted by the regular expression parser:

<p>
<div class="verbatim"><pre>
\a      \b      \f      \n
\r      \t      \v      \x
\\
</pre></div>

<p>
Octal escapes are included in a limited form: If the first digit is a
0, or if there are three octal digits, it is considered an octal
escape. Otherwise, it is a group reference.  As for string literals,
octal escapes are always at most three digits in length.

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="4.2 re  "
  href="module-re.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="4.2 re  "
  href="module-re.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="4.2.2 matching vs Searching"
  href="matching-searching.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python库参考</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">前一节:</b>
<a class="sectref" rel="prev" href="module-re.html">4.2 re  </a>
<b class="navlabel">上一级:</b>
<a class="sectref" rel="parent" href="module-re.html">4.2 re  </a>
<b class="navlabel">下一节:</b>
<a class="sectref" rel="next" href="matching-searching.html">4.2.2 匹配 vs 查找</a>
</div>
</div>
<hr />
<span class="release-info">2.5.1 版本，文档更新在 2007，4，18。翻译：<a href="http://www.zkfarmer.org">zkfarmer</a>。</span>
</div>
<!--End of Navigation Panel-->
<address>
关于建议修改的信息，查阅 <i><a href="about.html">关于这个文档...</a></i> 。
</address>
</body>
</html>
